package com.entity.common;

import lombok.Data;

/**
 * 统一返回结果类
 */
@Data
public class ApiResult {

    /**是否成功*/
    private boolean success;

    /**返回码*/
    private String code;

    /**返回信息*/
    private String msg;

    /**返回数据*/
    private Object data;

    public  ApiResult(int i) {
        if(i>0){
            this.success=true;
            this.msg = getMsg(Thread.currentThread().getStackTrace()[2].getMethodName())+"成功！";
        }
        else{
            this.success=false;
            this.msg = getMsg(Thread.currentThread().getStackTrace()[2].getMethodName())+"失败";
        }
    }

    public ApiResult(boolean success, String msg) {
        this.success = success;
        this.msg = msg;
    }

    public ApiResult(boolean success, String msg, Object data) {
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    public ApiResult(boolean success, String code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private static String getMsg(String str){
        if(str.contains("add") || str.contains("save")){
            return  "新增";
        }
        else if(str.contains("delete")){
            return  "删除";
        }
        else if(str.contains("update")|| str.contains("edit")){
            return  "修改";
        }
        else{
            return "";
        }
    }

}
